<template>
  <div
    class="square-list"
    ref="square"
  >

    <div
      v-masonry="containerId"
      transition-duration="0.3s"
      item-selector=".item"
    >
      <div
        v-masonry-tile
        class="item"
        v-for="(s) in squareList"
        :key="s.id"
      >
        <div class="square-pic">
          <img :src="s.img_url">
        </div>
        <div class="square-text">
          <p class="content">{{s.content}}</p>
          <p>{{s.user.user_name}}</p>
          <div>
            <p>{{s.input_date}}</p>
            <p>{{s.addr}}</p>
          </div>
        </div>
      </div>
    </div>
    <wd-loading
      color="#000000"
      v-if="loading"
      class="loading"
    ></wd-loading>
    <wd-infinite-load
      ref="infinite"
      @loadmore="loadmore"
      :loading="refresh"
      v-if="squareList.length"
    ></wd-infinite-load>
  </div>
</template>

<script>
export default {
  name: "SquareList",

  data() {
    return {
      containerId: "",
      loading: true,
      refresh: false,
      squareList: [],
      scrollTop: "",
    };
  },

  mounted() {
    this.getSquareListData();
    this.$nextTick(() => {
      document
        .querySelector(".square-list")
        .addEventListener("scroll", this.handleScroll);
    });
  },
  watch: {
    // scroll(old, n){
    //   console.log(old,n);
    // }
  },
  methods: {
    // 获取小记列表数据
    getSquareListData() {
      this.refresh = true;
      this.$axios
        .get("https://apis.netstart.cn/one/diary/square/more/0")
        .then(({ data }) => {
          console.log(data.data.list);
          this.squareList = data.data.list;
          // 数据加载完毕
          this.$nextTick(() => {
            this.loading = false;
            this.refresh = false;
          });
        })
        .catch(() => {
          this.$refs.infinite.loadEnd();
        });
    },
    // 监听滚动条
    handleScroll() {
      this.scrollTop = this.$refs.square.scrollTop;
      console.log(this.scrollTop);
    },

    // 下拉加载更多
    loadmore() {
      let squareHeight = this.$refs.square.offsetHeight - 50;
      if (this.scrollTop >= squareHeight) {
        console.log("我是元素高度" + squareHeight);
        // this.$refs.loadmore.reset()
        // this.refresh = true;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.square-list {
  position: fixed;
  top: 42px;
  left: 0;
  bottom: 0px;
  padding: 0 0 50px 10px;
  width: 100vw;
  background-color: #fff;
  overflow: auto;
  box-sizing: border-box;

  .item {
    margin-top: 10px;
    margin-right: 10px;
    width: calc(47vw - 5px);
    border: 1px solid rgb(248, 241, 241);
    border-radius: 10px;
    box-shadow: 0px 1px 2px rgb(153 139 139);
    overflow: hidden;
    .square-pic {
      img {
        display: block;
        width: 100%;
      }
    }
    .square-text {
      padding: 10px 5px;
      p {
        font-size: 12px;
        line-height: 20px;
        color: #ccc;
      }
      .content {
        margin-bottom: 10px;
        width: 160px;
        font-size: 14px;
        color: #000;

        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }
}
.loading {
  margin: 20px auto;
}
</style>